package com.luyue.zagj.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.luyue.zagj.BaseActivity;
import com.luyue.zagj.MainActivity;
import com.luyue.zagj.R;
import com.luyue.zagj.utils.CharacterFormatUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterPswProActivity extends BaseActivity {

    @Bind(R.id.register_pswPro_cancel_btn)
    Button registerPswProCancelBtn;
    @Bind(R.id.register_pswPro_top_rl)
    RelativeLayout registerPswProTopRl;
    @Bind(R.id.register_pswPro_name_ed)
    EditText registerPswProNameEd;
    @Bind(R.id.register_pswPro_idcard_ed)
    EditText registerPswProIdcardEd;
    @Bind(R.id.register_pswPro_first_sp)
    Spinner registerPswProFirstSp;
    @Bind(R.id.register_pswPro_first_et)
    EditText registerPswProFirstEt;
    @Bind(R.id.register_pswPro_second_sp)
    Spinner registerPswProSecondSp;
    @Bind(R.id.register_pswPro_second_sp_et)
    EditText registerPswProSecondSpEt;
    @Bind(R.id.register_pswPro_next_btn)
    Button registerPswProNextBtn;
    @Bind(R.id.register_to_login_tv)
    LinearLayout registerToLoginTv;

    private List<String> list;
    private List<String> list2;
    private List<String> list3;
    private ArrayAdapter adpter;
    private ArrayAdapter adpter2;
    private String name;
    private String idcard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_psw_pro);
        ButterKnife.bind(this);

        list = new ArrayList<String>();
        list.add("请选择问题");
        list.add("text1");
        list.add("text2");
        list.add("text3");

        list2 = new ArrayList<String>();
        list2.add("请选择问题");
        list2.add("text3");
        list2.add("text4");
        list2.add("text5");


        adpter = new ArrayAdapter(this, R.layout.spinner_item, R.id.textId, list);
        adpter2 = new ArrayAdapter(this, R.layout.spinner_item, R.id.textId, list2);
        registerPswProFirstSp.setAdapter(adpter);
        //FindPswPro2Activity
        registerPswProFirstSp.setPrompt("选择问题");
        registerPswProFirstSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String str = parent.getItemAtPosition(position).toString();
                //          Toast.makeText(RegisterPswProActivity.this, "你点击的是:"+str, Toast.LENGTH_SHORT).show();
                //list2.remove(position);
                //adpter2.notifyDataSetChanged();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        registerPswProSecondSp.setAdapter(adpter2);
        registerPswProSecondSp.setPrompt("选择问题");
        registerPswProSecondSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String str = parent.getItemAtPosition(position).toString();
                //       Toast.makeText(RegisterPswProActivity.this, "你点击的是:"+str, Toast.LENGTH_SHORT).show();
//                list2.remove(position);
//                adpter2.notifyDataSetChanged();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    @OnClick({R.id.register_pswPro_cancel_btn, R.id.register_pswPro_next_btn, R.id.register_to_login_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_pswPro_cancel_btn:
                onBackPressed();
                break;
            case R.id.register_pswPro_next_btn:
                boolean checkMsg = checkMsg();
                if (checkMsg) {
                    Intent it = new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(it);
                    Toast.makeText(RegisterPswProActivity.this, "注册成功,，已自动登录", Toast.LENGTH_SHORT).show();
                    break;
                }
                break;
            case R.id.register_to_login_tv:

                Intent it = new Intent(this, LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(it);


        }
    }


    /**
     * 校验身份证合格性
     * @return
     */
    private boolean checkMsg() {
        name = registerPswProNameEd.getText().toString().trim();
        idcard = registerPswProIdcardEd.getText().toString().trim();
        if (null == name || "".equals(name)) {
            Toast.makeText(this, "名字不能为空", Toast.LENGTH_SHORT).show();
            return false;
        } else if (null == idcard || "".equals(idcard)) {
            Toast.makeText(this, "身份证不能为空", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!CharacterFormatUtil.isPersonIdValid(idcard)) {
            Toast.makeText(this, "身份证格式不正确", Toast.LENGTH_SHORT).show();
            return false;
    }
        return true;
    }
}
